﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    // NOTE: If you change the class name "MarkWork" here, you must also update the reference to "MarkWork" in App.config.
    public partial class MainService : IMarkWork
    {
        public DataSet MarkWork_GetList(out int itemCount, int currPage,int pageSize, int EmployeeID, DateTime _Date)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_MarkWork_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;

            dbCmd.Parameters.AddWithValue("@CurrentPage", currPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            dbCmd.Parameters.AddWithValue("@EmployeeID", EmployeeID);
            dbCmd.Parameters.AddWithValue("@Date", _Date);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                itemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool MarkWork_CheckExistEmployee(int EmployeeID, DateTime _date)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_MarkWork_CheckExistEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeID", EmployeeID);
            dbCmd.Parameters.AddWithValue("@Date", _date);
            try
            {
                dbConn.Open();
                int Count = Convert.ToInt32(dbCmd.ExecuteScalar());
                retVal = Count > 0 ? true : false;

            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet MarkWork_GetWithEmployee(int EmployeeID, DateTime _date)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_MarkWork_GetWithEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeID", EmployeeID);
            dbCmd.Parameters.AddWithValue("@Date", _date);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet MarkWork_CountDateWorkNewMember(int EmployeeID, DateTime _date)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_MarkWork_CountDateWorkNewMember", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeID", EmployeeID);
            dbCmd.Parameters.AddWithValue("@Date", _date);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet MarkWork_GetEmployeeInfoByDate(int EmployeeID, DateTime _date)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_MarkWork_GetEmployeeInfoByDate", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeID", EmployeeID);
            dbCmd.Parameters.AddWithValue("@Date", _date);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);

            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public int MarkWork_InsertAuto(MarkWorkInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_MarkWork_InsertAuto", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeID", eInfo.EmployeeID);
            if (eInfo.CheckDate != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@checkdate", eInfo.CheckDate);
            else
                dbCmd.Parameters.AddWithValue("@checkdate", DBNull.Value);

            if (eInfo.TimeInB != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@checkdateB", eInfo.TimeInB);
            else
                dbCmd.Parameters.AddWithValue("@checkdateB", DBNull.Value);

            dbCmd.Parameters.AddWithValue("@status", eInfo.CheckStatus);
            dbCmd.Parameters.AddWithValue("@CheckIsInsert", eInfo.CheckIsInsert);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public int MarkWork_Insert(MarkWorkInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_MarkWork_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@ShiftID", eInfo.ShiftID);
            dbCmd.Parameters.AddWithValue("@Date", eInfo.CheckDate);
            dbCmd.Parameters.AddWithValue("@TimeIn", eInfo.TimeIn);
            dbCmd.Parameters.AddWithValue("@TimeOut", eInfo.TimeOut);
            if (eInfo.TimeInB == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@TimeInB", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@TimeInB", eInfo.TimeInB);
            if (eInfo.TimeOutB == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@TimeOutB", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@TimeOutB", eInfo.TimeOutB);
            
            dbCmd.Parameters.AddWithValue("@OverTime", eInfo.OverTime);
            dbCmd.Parameters.AddWithValue("@NightOverTime", eInfo.NightOverTime);
            dbCmd.Parameters.AddWithValue("@NightShiftTime", eInfo.NightShiftTime);
            dbCmd.Parameters.AddWithValue("@OffTime", eInfo.OffTime);
            dbCmd.Parameters.AddWithValue("@HolidayTime", eInfo.HolidayTime);
            dbCmd.Parameters.AddWithValue("@WorkTime", eInfo.WorkTime);
            dbCmd.Parameters.AddWithValue("@Cong", eInfo.Cong);
            dbCmd.Parameters.AddWithValue("@IsWorking", eInfo.IsWorking);

            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool MarkWork_Update(MarkWorkInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_MarkWork_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@MarkWorkID", eInfo.ID);
            dbCmd.Parameters.AddWithValue("@EmployeeID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@ShiftID", eInfo.ShiftID);
            dbCmd.Parameters.AddWithValue("@Date", eInfo.CheckDate);
            dbCmd.Parameters.AddWithValue("@TimeIn", eInfo.TimeIn);
            dbCmd.Parameters.AddWithValue("@TimeOut", eInfo.TimeOut);
            if (eInfo.TimeInB == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@TimeInB", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@TimeInB", eInfo.TimeInB);
            if (eInfo.TimeOutB == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@TimeOutB", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@TimeOutB", eInfo.TimeOutB);
            dbCmd.Parameters.AddWithValue("@OverTime", eInfo.OverTime);
            dbCmd.Parameters.AddWithValue("@NightOverTime", eInfo.NightOverTime);
            dbCmd.Parameters.AddWithValue("@NightShiftTime", eInfo.NightShiftTime);
            dbCmd.Parameters.AddWithValue("@OffTime", eInfo.OffTime);
            dbCmd.Parameters.AddWithValue("@HolidayTime", eInfo.HolidayTime);
            dbCmd.Parameters.AddWithValue("@WorkTime", eInfo.WorkTime);
            dbCmd.Parameters.AddWithValue("@Cong", eInfo.Cong);
            dbCmd.Parameters.AddWithValue("@IsWorking", eInfo.IsWorking);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool MarkWork_UpdateOverTime(MarkWorkInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_MarkWork_UpdateOverTime", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@MarkWork_ID", eInfo.ID);
            dbCmd.Parameters.AddWithValue("@MarkWork_OverTime", eInfo.OverTime);

            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public MarkWorkInfo MarkWork_GetInfo(int MarkWorkID)
        {
            MarkWorkInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_MarkWork_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@MarkWork_ID", MarkWorkID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new MarkWorkInfo();
                    retVal.ID = Convert.ToInt32(dr["MarkWork_ID"]);
                    retVal.EmployeeID = Convert.ToInt32(dr["MarkWork_EmployeeID"]);
                    retVal.ShiftID = Convert.ToInt32(dr["MarkWork_ShiftID"]);
                    retVal.CheckDate = Convert.ToDateTime(dr["MarkWork_Date"]);
                    retVal.TimeIn = Convert.ToDateTime(dr["MarkWork_TimeIn"]);
                    retVal.TimeOut = Convert.ToDateTime(dr["MarkWork_TimeOut"]);
                    retVal.TimeInB = Convert.ToDateTime(dr["MarkWork_TimeInB"]);
                    retVal.TimeOutB = Convert.ToDateTime(dr["MarkWork_TimeOutB"]);
                    if (dr["MarkWork_OverTime"] != DBNull.Value)
                        retVal.OverTime = Convert.ToDecimal(dr["MarkWork_OverTime"]);
                    else
                        retVal.OverTime = 0;
                    if (dr["MarkWork_WorkTime"] != DBNull.Value)
                        retVal.WorkTime = Convert.ToDecimal(dr["MarkWork_WorkTime"]);
                    else
                        retVal.WorkTime = 0;
                    if (dr["MarkWork_Cong"] != DBNull.Value)
                        retVal.Cong = Convert.ToDecimal(dr["MarkWork_Cong"]);
                    else
                        retVal.Cong = 0;
                    retVal.IsWorking = Convert.ToBoolean(dr["MarkWork_IsWorking"]);
                    retVal.StatusWorking = dr["MarkWork_StatusWorking"].ToString();
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }
    }
}
